home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / WaveTableOscControl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  65.0 KB  |  1,740 lines  |  [TEXT/KAHL]

  1. /* WaveTableOscControl.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveTableOscControl.h"
  31. #include "FastFixedPoint.h"
  32. #include "Memory.h"
  33. #include "EnvelopeState.h"
  34. #include "SampleConsts.h"
  35. #include "LFOGenerator.h"
  36. #include "Multisampler.h"
  37. #include "OscillatorSpecifier.h"
  38. #include "Envelope.h"
  39. #include "LFOListSpecifier.h"
  40. #include "FastModulation.h"
  41. #include "64BitMath.h"
  42. #include "ErrorDaemon.h"
  43.  
  44.  
  45. /* prototypes for fast playback routines */
  46. static void                        Wave_Mono_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  47.                                                 long SampleCount, largefixedsigned* RawBuffer);
  48. static void                        Wave_Stereo_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  49.                                                 long SampleCount, largefixedsigned* RawBuffer);
  50. static void                        Wave_Mono_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  51.                                                 long SampleCount, largefixedsigned* RawBuffer);
  52. static void                        Wave_Stereo_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  53.                                                 long SampleCount, largefixedsigned* RawBuffer);
  54.  
  55. static void                        Wave_Mono_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  56.                                                 long SampleCount, largefixedsigned* RawBuffer);
  57. static void                        Wave_Stereo_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  58.                                                 long SampleCount, largefixedsigned* RawBuffer);
  59. static void                        Wave_Mono_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  60.                                                 long SampleCount, largefixedsigned* RawBuffer);
  61. static void                        Wave_Stereo_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  62.                                                 long SampleCount, largefixedsigned* RawBuffer);
  63.  
  64. static void                        Wave_Mono_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  65.                                                 long SampleCount, largefixedsigned* RawBuffer);
  66. static void                        Wave_Stereo_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  67.                                                 long SampleCount, largefixedsigned* RawBuffer);
  68. static void                        Wave_Mono_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  69.                                                 long SampleCount, largefixedsigned* RawBuffer);
  70. static void                        Wave_Stereo_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  71.                                                 long SampleCount, largefixedsigned* RawBuffer);
  72.  
  73. static void                        Wave_NoOutput(WaveTableStateRec* State,
  74.                                                 long SampleCount, largefixedsigned* RawBuffer);
  75.  
  76.  
  77. /* wave table oscillator template information record */
  78. struct WaveTableTemplateRec
  79.     {
  80.         /* source information for the wave table */
  81.         MultiSampleRec*                WaveTableSourceSelector;
  82.  
  83.         /* sampling rate for final output */
  84.         long                                    FinalOutputSamplingRate;
  85.         /* number of envelope updates per second */
  86.         float                                    EnvelopeTicksPerSecond;
  87.  
  88.         /* values for scaling the frequency of something.  if we were really serious about */
  89.         /* this, we'd traverse all of the oscillators with integral multiples or harmonic */
  90.         /* fractions of the pitch & set their differentials to the same precision as the */
  91.         /* worst oscillator so that they would all stay in sync as time progressed. */
  92.         float                                    FrequencyMultiplier;
  93.  
  94.         /* stereo status */
  95.         NumChannelsType                StereoPlayback;
  96.         /* time interpolation flag */
  97.         MyBoolean                            InterpolateThroughTime;
  98.         /* inter-wave interpolation flag */
  99.         MyBoolean                            InterpolateAcrossWaves;
  100.         /* include in final output flag */
  101.         MyBoolean                            IncludeInFinalOutput;
  102.  
  103.         /* envelope templates */
  104.         EnvelopeRec*                    LoudnessEnvelopeTemplate;
  105.         LFOListSpecRec*                LoudnessLFOTemplate;
  106.         EnvelopeRec*                    IndexEnvelopeTemplate;
  107.         LFOListSpecRec*                IndexLFOTemplate;
  108.  
  109.         /* miscellaneous control parameters */
  110.         float                                    StereoBias;
  111.         float                                    TimeDisplacement;
  112.         float                                    OverallOscillatorLoudness;
  113.  
  114.         /* error logging facility */
  115.         ErrorDaemonRec*                ErrorDaemon;
  116.  
  117.         /* link for list control */
  118.         WaveTableTemplateRec*    Next;
  119.     };
  120.  
  121.  
  122. /* wave table oscillator state record */
  123. struct WaveTableStateRec
  124.     {
  125.         /* current sample position into the wave table */
  126.         LongLongRec                        WaveTableSamplePosition; /* 32-bit fixed point */
  127.         /* current increment value for the wave table sample position */
  128.         LongLongRec                        WaveTableSamplePositionDifferential; /* 32-bit fixed point */
  129.  
  130.         /* envelope tick countdown for pre-start time */
  131.         long                                    PreStartCountdown;
  132.  
  133.         /* function for generating a bunch of samples from the wave table */
  134.         void                                    (*WaveTableGenSamples)(WaveTableStateRec* State,
  135.                                                         long SampleCount, largefixedsigned* RawBuffer);
  136.  
  137.         /* number of frames per table */
  138.         long                                    FramesPerTable;
  139.         /* number of tables */
  140.         long                                    NumberOfTables;
  141.         /* raw wave table data array */
  142.         void**                                WaveTableMatrix;
  143.         /* number of bits in the data for the table */
  144.         NumBitsType                        TableNumBits;
  145.  
  146.         /* current index into table of waves.  0 = lowest wave table, NumberOfTables = highest */
  147.         FastFixedType                    WaveTableIndex;
  148.         /* envelope controlling wave table index */
  149.         EvalEnvelopeRec*            WaveTableIndexEnvelope;
  150.         /* LFO generators modifying the output of the index envelope generator */
  151.         LFOGenRec*                        IndexLFOGenerator;
  152.  
  153.         /* NOTE:  either OverallLoudness or Left/RightLoudness are used, but not both */
  154.         /* current overall loudness for oscillator */
  155.         FastFixedType                    OverallLoudness; /* 15-bit fixed point, 0..1 */
  156.         /* left channel loudness */
  157.         FastFixedType                    LeftLoudness; /* 15-bit fixed point, 0..1 */
  158.         /* right channel loudness */
  159.         FastFixedType                    RightLoudness; /* 15-bit fixed point, 0..1 */
  160.         /* panning position for splitting envelope generator into stereo channels */
  161.         /* 0 = left channel, 0.5 = middle, 1 = right channel */
  162.         FastFixedType                    Panning; /* 15-bit fixed point, -1..1 */
  163.         /* envelope that is generating the loudness information */
  164.         EvalEnvelopeRec*            WaveTableLoudnessEnvelope;
  165.         /* LFO generators modifying the output of the loudness envelope generator */
  166.         LFOGenRec*                        LoudnessLFOGenerator;
  167.  
  168.         /* this flag is True if the wave table data was defined at the specified pitch */
  169.         /* (and the wave table array is thus valid) or False if there is no wave table */
  170.         /* at this pitch (and the array is invalid) */
  171.         MyBoolean                            WaveTableWasDefined;
  172.  
  173.         /* this field contains the overall volume scaling for everything so that we */
  174.         /* can treat the envelopes as always going between 0 and 1. */
  175.         float                                    NoteLoudnessScaling;
  176.  
  177.         /* static information for the wave table */
  178.         WaveTableTemplateRec    Template; /* a copy of the data */
  179.  
  180.         /* link for list control */
  181.         WaveTableStateRec*        Next;
  182.     };
  183.  
  184.  
  185.  
  186.  
  187. static WaveTableTemplateRec*        WaveTableTemplateFreeList = NIL;
  188. static WaveTableStateRec*                WaveTableStateFreeList = NIL;
  189.  
  190.  
  191. /* get rid of all cached memory for state or template records */
  192. void                                    FlushWaveTableOscControl(void)
  193.     {
  194.         while (WaveTableTemplateFreeList != NIL)
  195.             {
  196.                 WaveTableTemplateRec*        Temp;
  197.  
  198.                 Temp = WaveTableTemplateFreeList;
  199.                 WaveTableTemplateFreeList = WaveTableTemplateFreeList->Next;
  200.                 ReleasePtr((char*)Temp);
  201.             }
  202.  
  203.         while (WaveTableStateFreeList != NIL)
  204.             {
  205.                 WaveTableStateRec*        Temp;
  206.  
  207.                 Temp = WaveTableStateFreeList;
  208.                 WaveTableStateFreeList = WaveTableStateFreeList->Next;
  209.                 ReleasePtr((char*)Temp);
  210.             }
  211.     }
  212.  
  213.  
  214. #if DEBUG
  215. static void                        CheckValidWaveTableTemplate(WaveTableTemplateRec* Object)
  216.     {
  217.         WaveTableTemplateRec*    Scan;
  218.  
  219.         Scan = WaveTableTemplateFreeList;
  220.         while (Scan != NIL)
  221.             {
  222.                 if (Scan == Object)
  223.                     {
  224.                         PRERR(ForceAbort,"CheckValidWaveTableTemplate:  template is on free list");
  225.                     }
  226.                 Scan = Scan->Next;
  227.             }
  228.     }
  229. #else
  230. #define CheckValidWaveTableTemplate(x) ((void)0)
  231. #endif
  232.  
  233.  
  234. #if DEBUG
  235. static void                        CheckValidWaveTableState(WaveTableStateRec* Object)
  236.     {
  237.         WaveTableStateRec*        Scan;
  238.  
  239.         Scan = WaveTableStateFreeList;
  240.         while (Scan != NIL)
  241.             {
  242.                 if (Scan == Object)
  243.                     {
  244.                         PRERR(ForceAbort,"CheckValidWaveTableState:  state is on free list");
  245.                     }
  246.                 Scan = Scan->Next;
  247.             }
  248.     }
  249. #else
  250. #define CheckValidWaveTableState(x) ((void)0)
  251. #endif
  252.  
  253.  
  254. /* perform one envelope update cycle */
  255. void                                    UpdateWaveTableEnvelopes(WaveTableStateRec* State)
  256.     {
  257.         FastFixedType                Temp;
  258.  
  259.         CheckPtrExistence(State);
  260.         CheckValidWaveTableState(State);
  261.  
  262.         /* this is for the benefit of resampling only -- envelope generators do their */
  263.         /* own pre-origin sequencing */
  264.         if (State->PreStartCountdown > 0)
  265.             {
  266.                 State->PreStartCountdown -= 1;
  267.             }
  268.  
  269.         Temp = (State->NumberOfTables - 1) * LFOGenUpdateCycle(State->IndexLFOGenerator,
  270.             EnvelopeUpdate(State->WaveTableIndexEnvelope));
  271.         if (Temp < 0)
  272.             {
  273.                 Temp = 0;
  274.             }
  275.         else if (Temp > Int2FastFixed(State->NumberOfTables - 1))
  276.             {
  277.                 Temp = Int2FastFixed(State->NumberOfTables - 1);
  278.             }
  279.         State->WaveTableIndex = Temp;
  280.  
  281.         Temp = State->NoteLoudnessScaling * LFOGenUpdateCycle(State->LoudnessLFOGenerator,
  282.             EnvelopeUpdate(State->WaveTableLoudnessEnvelope));
  283.         /* fast fixed has a very narrow range, so overflow can't be permitted: */
  284.         /* 15x15->30 bits, with 2 extra bits; we use one for sign and the other */
  285.         /* to permit the representation of 1 and -1. */
  286.         if (State->Template.StereoPlayback == eSampleMono)
  287.             {
  288.                 if (Temp < - Int2FastFixed(1))
  289.                     {
  290.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  291.                             - FastFixed2Float(Temp));
  292.                         Temp = - Int2FastFixed(1);
  293.                     }
  294.                 else if (Temp > Int2FastFixed(1))
  295.                     {
  296.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  297.                             FastFixed2Float(Temp));
  298.                         Temp = Int2FastFixed(1);
  299.                     }
  300.                 State->OverallLoudness = Temp;
  301.             }
  302.          else
  303.             {
  304.                 FastFixedType                LeftVolumeScaling;
  305.                 FastFixedType                RightVolumeScaling;
  306.                 FastFixedType                MaxVolScaling;
  307.                 FastFixedType                MaxTemp;
  308.  
  309.                 LeftVolumeScaling = FastFixedTimesFastFixedToFastFixed(Double2FastFixed(0.5),
  310.                     Int2FastFixed(1) - State->Panning);
  311.                 RightVolumeScaling = FastFixedTimesFastFixedToFastFixed(Double2FastFixed(0.5),
  312.                     Int2FastFixed(1) + State->Panning);
  313.  
  314.                 if (((LeftVolumeScaling >= 0) ? LeftVolumeScaling : (- LeftVolumeScaling))
  315.                     > ((RightVolumeScaling >= 0) ? RightVolumeScaling : (- RightVolumeScaling)))
  316.                     {
  317.                         MaxVolScaling = ((LeftVolumeScaling >= 0)
  318.                             ? LeftVolumeScaling : (- LeftVolumeScaling));
  319.                     }
  320.                  else
  321.                     {
  322.                         MaxVolScaling = ((RightVolumeScaling >= 0)
  323.                             ? RightVolumeScaling : (- RightVolumeScaling));
  324.                     }
  325.                 MaxTemp = Double2FastFixed((float)1 / FastFixed2Float(MaxVolScaling));
  326.                 if (Temp < - MaxTemp)
  327.                     {
  328.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  329.                             - FastFixed2Float(Temp) / FastFixed2Float(MaxTemp));
  330.                         Temp = - MaxTemp;
  331.                     }
  332.                 else if (Temp > MaxTemp)
  333.                     {
  334.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  335.                             FastFixed2Float(Temp) / FastFixed2Float(MaxTemp));
  336.                         Temp = MaxTemp;
  337.                     }
  338.  
  339.                 State->LeftLoudness = FastFixedTimesFastFixedToFastFixed(
  340.                     LeftVolumeScaling,Temp);
  341.                 State->RightLoudness = FastFixedTimesFastFixedToFastFixed(
  342.                     RightVolumeScaling,Temp);
  343.             }
  344.     }
  345.  
  346.  
  347. /* dispose of the wave table state record */
  348. void                                    DisposeWaveTableState(WaveTableStateRec* State)
  349.     {
  350.         CheckPtrExistence(State);
  351.         CheckValidWaveTableState(State);
  352.  
  353.         DisposeEnvelopeStateRecord(State->WaveTableIndexEnvelope);
  354.         DisposeLFOGenerator(State->IndexLFOGenerator);
  355.         DisposeEnvelopeStateRecord(State->WaveTableLoudnessEnvelope);
  356.         DisposeLFOGenerator(State->LoudnessLFOGenerator);
  357.  
  358.         State->Next = WaveTableStateFreeList;
  359.         WaveTableStateFreeList = State;
  360.     }
  361.  
  362.  
  363. /* dispose of the wave table information template */
  364. void                                    DisposeWaveTableTemplate(WaveTableTemplateRec* Template)
  365.     {
  366.         CheckPtrExistence(Template);
  367.         CheckValidWaveTableTemplate(Template);
  368.  
  369.         DisposeMultisample(Template->WaveTableSourceSelector);
  370.  
  371.         Template->Next = WaveTableTemplateFreeList;
  372.         WaveTableTemplateFreeList = Template;
  373.     }
  374.  
  375.  
  376. /* create a new wave table template */
  377. WaveTableTemplateRec*    NewWaveTableTemplate(struct OscillatorRec* Oscillator,
  378.                                                 float EnvelopeTicksPerSecond, long SamplingRate,
  379.                                                 MyBoolean Stereo, MyBoolean TimeInterp, MyBoolean WaveInterp,
  380.                                                 ErrorDaemonRec* ErrorDaemon)
  381.     {
  382.         WaveTableTemplateRec*    Template;
  383.  
  384.         CheckPtrExistence(ErrorDaemon);
  385.         CheckPtrExistence(Oscillator);
  386.         ERROR(OscillatorGetWhatKindItIs(Oscillator) != eOscillatorWaveTable,PRERR(ForceAbort,
  387.             "NewWaveTableTemplate:  oscillator is not a wave table"));
  388.  
  389.         if (WaveTableTemplateFreeList != NIL)
  390.             {
  391.                 Template = WaveTableTemplateFreeList;
  392.                 WaveTableTemplateFreeList = WaveTableTemplateFreeList->Next;
  393.             }
  394.          else
  395.             {
  396.                 Template = (WaveTableTemplateRec*)AllocPtrCanFail(sizeof(WaveTableTemplateRec),
  397.                     "WaveTableTemplateRec");
  398.                 if (Template == NIL)
  399.                     {
  400.                         return NIL;
  401.                     }
  402.             }
  403.         EXECUTE(Template->Next = (WaveTableTemplateRec*)0x81818181;)
  404.  
  405.         Template->WaveTableSourceSelector = NewMultisampleWaveTable(
  406.             OscillatorGetSampleIntervalList(Oscillator));
  407.         if (Template->WaveTableSourceSelector == NIL)
  408.             {
  409.              FailurePoint1:
  410.                 Template->Next = WaveTableTemplateFreeList;
  411.                 WaveTableTemplateFreeList = Template;
  412.                 return NIL;
  413.             }
  414.  
  415.         Template->FinalOutputSamplingRate = SamplingRate;
  416.         Template->EnvelopeTicksPerSecond = EnvelopeTicksPerSecond;
  417.         Template->OverallOscillatorLoudness = OscillatorGetOutputLoudness(Oscillator);
  418.  
  419.         /* it might be better to handle divisor and multiplier separately -- we would */
  420.         /* want to do that if we were trying to guarantee that all harmonic */
  421.         /* oscillators ran in lock-step */
  422.         Template->FrequencyMultiplier = OscillatorGetFrequencyMultiplier(Oscillator)
  423.             / OscillatorGetFrequencyDivisor(Oscillator);
  424.  
  425.         Template->StereoBias = OscillatorGetStereoBias(Oscillator);
  426.         Template->TimeDisplacement = OscillatorGetTimeDisplacement(Oscillator);
  427.  
  428.         if (Stereo)
  429.             {
  430.                 Template->StereoPlayback = eSampleStereo;
  431.             }
  432.          else
  433.             {
  434.                 Template->StereoPlayback = eSampleMono;
  435.             }
  436.         Template->InterpolateThroughTime = TimeInterp;
  437.         Template->InterpolateAcrossWaves = WaveInterp;
  438.         Template->IncludeInFinalOutput = IncludeOscillatorInFinalOutput(Oscillator);
  439.  
  440.         /* these are just references */
  441.         Template->LoudnessEnvelopeTemplate = OscillatorGetLoudnessEnvelope(Oscillator);
  442.         Template->LoudnessLFOTemplate = OscillatorGetLoudnessLFOList(Oscillator);
  443.         Template->IndexEnvelopeTemplate = OscillatorGetExcitationEnvelope(Oscillator);
  444.         Template->IndexLFOTemplate = OscillatorGetExcitationLFOList(Oscillator);
  445.  
  446.         /* another reference */
  447.         Template->ErrorDaemon = ErrorDaemon;
  448.  
  449.         return Template;
  450.     }
  451.  
  452.  
  453. /* create a new wave table state object. */
  454. WaveTableStateRec*        NewWaveTableState(WaveTableTemplateRec* Template,
  455.                                                 float FreqForMultisampling, float Accent1, float Accent2,
  456.                                                 float Accent3, float Accent4, float Loudness, float HurryUp,
  457.                                                 long* PreOriginTimeOut, float StereoPosition,
  458.                                                 float InitialFrequency)
  459.     {
  460.         WaveTableStateRec*    State;
  461.         long                                MaxPreOrigin;
  462.         long                                OnePreOrigin;
  463.  
  464.         CheckPtrExistence(Template);
  465.         CheckValidWaveTableTemplate(Template);
  466.  
  467.         if (WaveTableStateFreeList != NIL)
  468.             {
  469.                 State = WaveTableStateFreeList;
  470.                 WaveTableStateFreeList = WaveTableStateFreeList->Next;
  471.             }
  472.          else
  473.             {
  474.                 State = (WaveTableStateRec*)AllocPtrCanFail(sizeof(WaveTableStateRec),
  475.                     "WaveTableStateRec");
  476.                 if (State == NIL)
  477.                     {
  478.                      FailurePoint1:
  479.                         return NIL;
  480.                     }
  481.             }
  482.         EXECUTE(State->Next = (WaveTableStateRec*)0x81818181;)
  483.  
  484.         State->Template = *Template;
  485.  
  486.         MaxPreOrigin = 0;
  487.  
  488.         Double2LongLong(0,State->WaveTableSamplePosition);
  489.         /* State->WaveTableSamplePositionDifferential specified in separate call */
  490.  
  491.         State->NoteLoudnessScaling = Loudness * Template->OverallOscillatorLoudness;
  492.  
  493.         State->WaveTableWasDefined = GetMultisampleReferenceWaveTable(
  494.             Template->WaveTableSourceSelector,FreqForMultisampling,&(State->WaveTableMatrix),
  495.             &(State->FramesPerTable),&(State->NumberOfTables),&(State->TableNumBits));
  496.  
  497.         /* Template->IncludeInFinalOutput: */
  498.         /*   the routine pointer is not used for modulation */
  499.         /* State->WaveTableWasDefined: */
  500.         /*   if there is no wave table defined for the current pitch, then we don't */
  501.         /*   bother generating any data */
  502.         /* State->FramesPerTable > 0: */
  503.         /*   if the wave table is empty, then we don't do any work (and we must not, */
  504.         /*   since array accesses would cause a crash) */
  505.         if (Template->IncludeInFinalOutput && State->WaveTableWasDefined
  506.             && (State->FramesPerTable > 0))
  507.             {
  508.                 if (Template->StereoPlayback == eSampleStereo)
  509.                     {
  510.                         if (State->TableNumBits == eSample16bit)
  511.                             {
  512.                                 if (Template->InterpolateAcrossWaves)
  513.                                     {
  514.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_YesTime_YesWave;
  515.                                     }
  516.                                 else if (Template->InterpolateThroughTime)
  517.                                     {
  518.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_YesTime_NoWave;
  519.                                     }
  520.                                 else
  521.                                     {
  522.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_NoTime_NoWave;
  523.                                     }
  524.                             }
  525.                          else
  526.                             {
  527.                                 if (Template->InterpolateAcrossWaves)
  528.                                     {
  529.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_YesTime_YesWave;
  530.                                     }
  531.                                 else if (Template->InterpolateThroughTime)
  532.                                     {
  533.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_YesTime_NoWave;
  534.                                     }
  535.                                 else
  536.                                     {
  537.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_NoTime_NoWave;
  538.                                     }
  539.                             }
  540.                     }
  541.                  else
  542.                     {
  543.                         if (State->TableNumBits == eSample16bit)
  544.                             {
  545.                                 if (Template->InterpolateAcrossWaves)
  546.                                     {
  547.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_YesTime_YesWave;
  548.                                     }
  549.                                 else if (Template->InterpolateThroughTime)
  550.                                     {
  551.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_YesTime_NoWave;
  552.                                     }
  553.                                 else
  554.                                     {
  555.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_NoTime_NoWave;
  556.                                     }
  557.                             }
  558.                          else
  559.                             {
  560.                                 if (Template->InterpolateAcrossWaves)
  561.                                     {
  562.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_YesTime_YesWave;
  563.                                     }
  564.                                 else if (Template->InterpolateThroughTime)
  565.                                     {
  566.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_YesTime_NoWave;
  567.                                     }
  568.                                 else
  569.                                     {
  570.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_NoTime_NoWave;
  571.                                     }
  572.                             }
  573.                     }
  574.             }
  575.          else
  576.             {
  577.                 State->WaveTableGenSamples = &Wave_NoOutput;
  578.             }
  579.  
  580.         State->PreStartCountdown = Template->TimeDisplacement
  581.             * Template->EnvelopeTicksPerSecond + 0.5;
  582.         if (- State->PreStartCountdown > MaxPreOrigin)
  583.             {
  584.                 MaxPreOrigin = - State->PreStartCountdown;
  585.             }
  586.  
  587.         /* State->WaveTableIndex determined by envelope update */
  588.         State->WaveTableIndexEnvelope = NewEnvelopeStateRecord(
  589.             Template->IndexEnvelopeTemplate,Accent1,Accent2,Accent3,Accent4,InitialFrequency,
  590.             1,HurryUp,Template->EnvelopeTicksPerSecond,&OnePreOrigin);
  591.         if (State->WaveTableIndexEnvelope == NIL)
  592.             {
  593.              FailurePoint2:
  594.                 State->Next = WaveTableStateFreeList;
  595.                 WaveTableStateFreeList = State;
  596.                 goto FailurePoint1;
  597.             }
  598.         if (OnePreOrigin > MaxPreOrigin)
  599.             {
  600.                 MaxPreOrigin = OnePreOrigin;
  601.             }
  602.         State->IndexLFOGenerator = NewLFOGenerator(Template->IndexLFOTemplate,
  603.             &OnePreOrigin,Accent1,Accent2,Accent3,Accent4,InitialFrequency,HurryUp,
  604.             Template->EnvelopeTicksPerSecond,1,1,eLFOArithDefault,FreqForMultisampling);
  605.         if (State->IndexLFOGenerator == NIL)
  606.             {
  607.              FailurePoint3:
  608.                 DisposeEnvelopeStateRecord(State->WaveTableIndexEnvelope);
  609.                 goto FailurePoint2;
  610.             }
  611.         if (OnePreOrigin > MaxPreOrigin)
  612.             {
  613.                 MaxPreOrigin = OnePreOrigin;
  614.             }
  615.  
  616.         /* State->OverallLoudness, State->LeftLoudness, State->RightLoudness */
  617.         /* are determined by the envelope update */
  618.         StereoPosition += Template->StereoBias;
  619.         if (StereoPosition < -1)
  620.             {
  621.                 StereoPosition = -1;
  622.             }
  623.         else if (StereoPosition > 1)
  624.             {
  625.                 StereoPosition = 1;
  626.             }
  627.         State->Panning = Double2FastFixed(StereoPosition);
  628.         State->WaveTableLoudnessEnvelope = NewEnvelopeStateRecord(
  629.             Template->LoudnessEnvelopeTemplate,Accent1,Accent2,Accent3,Accent4,
  630.             InitialFrequency,1,HurryUp,Template->EnvelopeTicksPerSecond,&OnePreOrigin);
  631.         if (State->WaveTableLoudnessEnvelope == NIL)
  632.             {
  633.              FailurePoint4:
  634.                 DisposeLFOGenerator(State->IndexLFOGenerator);
  635.                 goto FailurePoint3;
  636.             }
  637.         if (OnePreOrigin > MaxPreOrigin)
  638.             {
  639.                 MaxPreOrigin = OnePreOrigin;
  640.             }
  641.         State->LoudnessLFOGenerator = NewLFOGenerator(Template->LoudnessLFOTemplate,
  642.             &OnePreOrigin,Accent1,Accent2,Accent3,Accent4,InitialFrequency,HurryUp,
  643.             Template->EnvelopeTicksPerSecond,1,1,eLFOArithDefault,FreqForMultisampling);
  644.         if (State->LoudnessLFOGenerator == NIL)
  645.             {
  646.              FailurePoint5:
  647.                 DisposeEnvelopeStateRecord(State->WaveTableLoudnessEnvelope);
  648.                 goto FailurePoint4;
  649.             }
  650.         if (OnePreOrigin > MaxPreOrigin)
  651.             {
  652.                 MaxPreOrigin = OnePreOrigin;
  653.             }
  654.  
  655.         *PreOriginTimeOut = MaxPreOrigin;
  656.         return State;
  657.     }
  658.  
  659.  
  660. /* fix up pre-origin time for the wave table state object */
  661. void                                    FixUpWaveTableStatePreOrigin(WaveTableStateRec* State,
  662.                                                 long ActualPreOrigin)
  663.     {
  664.         CheckPtrExistence(State);
  665.         CheckValidWaveTableState(State);
  666.  
  667.         EnvelopeStateFixUpInitialDelay(State->WaveTableIndexEnvelope,ActualPreOrigin);
  668.         EnvelopeStateFixUpInitialDelay(State->WaveTableLoudnessEnvelope,ActualPreOrigin);
  669.         LFOGeneratorFixEnvelopeOrigins(State->IndexLFOGenerator,ActualPreOrigin);
  670.         LFOGeneratorFixEnvelopeOrigins(State->LoudnessLFOGenerator,ActualPreOrigin);
  671.  
  672.         State->PreStartCountdown += ActualPreOrigin;
  673.     }
  674.  
  675.  
  676. /* set a new frequency for a wave table state object.  used for portamento */
  677. /* and modulation of frequency (vibrato) */
  678. void                                    WaveTableStateNewFrequency(WaveTableStateRec* State,
  679.                                                 float NewFrequencyHertz)
  680.     {
  681.         CheckPtrExistence(State);
  682.         CheckValidWaveTableState(State);
  683.  
  684.         Double2LongLong((NewFrequencyHertz * State->Template.FrequencyMultiplier)
  685.             / State->Template.FinalOutputSamplingRate * State->FramesPerTable,
  686.             State->WaveTableSamplePositionDifferential);
  687.     }
  688.  
  689.  
  690. /* send a key-up signal to one of the oscillators */
  691. void                                    WaveTableKeyUpSustain1(WaveTableStateRec* State)
  692.     {
  693.         CheckPtrExistence(State);
  694.         CheckValidWaveTableState(State);
  695.         LFOGeneratorKeyUpSustain1(State->IndexLFOGenerator);
  696.         LFOGeneratorKeyUpSustain1(State->LoudnessLFOGenerator);
  697.         EnvelopeKeyUpSustain1(State->WaveTableIndexEnvelope);
  698.         EnvelopeKeyUpSustain1(State->WaveTableLoudnessEnvelope);
  699.     }
  700.  
  701.  
  702. /* send a key-up signal to one of the oscillators */
  703. void                                    WaveTableKeyUpSustain2(WaveTableStateRec* State)
  704.     {
  705.         CheckPtrExistence(State);
  706.         CheckValidWaveTableState(State);
  707.         LFOGeneratorKeyUpSustain2(State->IndexLFOGenerator);
  708.         LFOGeneratorKeyUpSustain2(State->LoudnessLFOGenerator);
  709.         EnvelopeKeyUpSustain2(State->WaveTableIndexEnvelope);
  710.         EnvelopeKeyUpSustain2(State->WaveTableLoudnessEnvelope);
  711.     }
  712.  
  713.  
  714. /* send a key-up signal to one of the oscillators */
  715. void                                    WaveTableKeyUpSustain3(WaveTableStateRec* State)
  716.     {
  717.         CheckPtrExistence(State);
  718.         CheckValidWaveTableState(State);
  719.         LFOGeneratorKeyUpSustain3(State->IndexLFOGenerator);
  720.         LFOGeneratorKeyUpSustain3(State->LoudnessLFOGenerator);
  721.         EnvelopeKeyUpSustain3(State->WaveTableIndexEnvelope);
  722.         EnvelopeKeyUpSustain3(State->WaveTableLoudnessEnvelope);
  723.     }
  724.  
  725.  
  726. /* restart a wave table oscillator.  this is used for tie continuations */
  727. void                                    RestartWaveTableState(WaveTableStateRec* State,
  728.                                                 float NewFreqMultisampling, float NewAccent1, float NewAccent2,
  729.                                                 float NewAccent3, float NewAccent4, float NewLoudness,
  730.                                                 float NewHurryUp, MyBoolean RetriggerEnvelopes,
  731.                                                 float NewStereoPosition, float NewInitialFrequency)
  732.     {
  733.         CheckPtrExistence(State);
  734.         CheckValidWaveTableState(State);
  735.  
  736.         NewStereoPosition += State->Template.StereoBias;
  737.         if (NewStereoPosition < -1)
  738.             {
  739.                 NewStereoPosition = -1;
  740.             }
  741.         else if (NewStereoPosition > 1)
  742.             {
  743.                 NewStereoPosition = 1;
  744.             }
  745.         State->Panning = Double2FastFixed(NewStereoPosition);
  746.  
  747.         State->NoteLoudnessScaling = NewLoudness * State->Template.OverallOscillatorLoudness;
  748.  
  749.         EnvelopeRetriggerFromOrigin(State->WaveTableIndexEnvelope,NewAccent1,NewAccent2,
  750.             NewAccent3,NewAccent4,NewInitialFrequency,1,NewHurryUp,
  751.             State->Template.EnvelopeTicksPerSecond,RetriggerEnvelopes);
  752.         EnvelopeRetriggerFromOrigin(State->WaveTableLoudnessEnvelope,NewAccent1,
  753.             NewAccent2,NewAccent3,NewAccent4,NewInitialFrequency,1,NewHurryUp,
  754.             State->Template.EnvelopeTicksPerSecond,RetriggerEnvelopes);
  755.         LFOGeneratorRetriggerFromOrigin(State->IndexLFOGenerator,NewAccent1,NewAccent2,
  756.             NewAccent3,NewAccent4,NewInitialFrequency,NewHurryUp,
  757.             State->Template.EnvelopeTicksPerSecond,1,1,RetriggerEnvelopes);
  758.         LFOGeneratorRetriggerFromOrigin(State->LoudnessLFOGenerator,NewAccent1,
  759.             NewAccent2,NewAccent3,NewAccent4,NewInitialFrequency,NewHurryUp,
  760.             State->Template.EnvelopeTicksPerSecond,1,1,RetriggerEnvelopes);
  761.     }
  762.  
  763.  
  764. /* generate one sequence of samples */
  765. void                                    WaveTableGenSamples(WaveTableStateRec* State,
  766.                                                 long SampleCount, largefixedsigned* RawBuffer)
  767.     {
  768.         CheckPtrExistence(State);
  769.         CheckValidWaveTableState(State);
  770.  
  771.         if (State->PreStartCountdown <= 0)
  772.             {
  773.                 (*State->WaveTableGenSamples)(State,SampleCount,RawBuffer);
  774.             }
  775.     }
  776.  
  777.  
  778. /* find out if the wave table oscillator has finished */
  779. MyBoolean                            WaveTableIsItFinished(WaveTableStateRec* State)
  780.     {
  781.         CheckPtrExistence(State);
  782.         CheckValidWaveTableState(State);
  783.  
  784.         /* we are finished when one of the following conditions is met: */
  785.         /*  - output volume is zero AND loudness envelope is finished */
  786.         /*  - we are not generating any signal */
  787.         if (!State->WaveTableWasDefined)
  788.             {
  789.                 return True;
  790.             }
  791.         if (IsEnvelopeAtEnd(State->WaveTableLoudnessEnvelope))
  792.             {
  793.                 if (State->Template.StereoPlayback == eSampleStereo)
  794.                     {
  795.                         if ((State->LeftLoudness == 0) && (State->RightLoudness == 0))
  796.                             {
  797.                                 return True;
  798.                             }
  799.                     }
  800.                  else
  801.                     {
  802.                         if (State->OverallLoudness == 0)
  803.                             {
  804.                                 return True;
  805.                             }
  806.                     }
  807.             }
  808.         return IsEnvelopeAtEnd(State->WaveTableLoudnessEnvelope);
  809.     }
  810.  
  811.  
  812. /* generate a single sample (called for modulation chains) */
  813. /* OutputPlace should have 1 entry for mono output or 2 entries for stereo output */
  814. float                                    WaveTableGenOneSample(WaveTableStateRec* State,
  815.                                                 ModulationTypes* PhaseGenModulateHow,
  816.                                                 float* PhaseGenModulationScaling,
  817.                                                 float* PhaseGenModulationOrigin,
  818.                                                 float* PhaseGenOldValues,
  819.                                                 long* PhaseGenIndirectionTable,
  820.                                                 long NumberOfPhaseGenModulators,
  821.                                                 ModulationTypes* OutputModulateHow,
  822.                                                 float* OutputModulationScaling,
  823.                                                 float* OutputModulationOrigin,
  824.                                                 float* OutputOldValues,
  825.                                                 long* OutputIndirectionTable,
  826.                                                 long NumberOfOutputModulators,
  827.                                                 largefixedsigned* OutputPlace)
  828.     {
  829.         float                                ReturnValue;
  830.         LongLongRec                    FrameIndex;
  831.         signed long                    Final; /* both 16-bit int & largefixedsigned */
  832.  
  833.         CheckPtrExistence(State);
  834.         CheckValidWaveTableState(State);
  835.  
  836.         /* should we bother doing any work? */
  837.         if (!State->WaveTableWasDefined || (State->FramesPerTable == 0)
  838.             || (State->PreStartCountdown > 0))
  839.             {
  840.                 /* modulate against zero */
  841.                 if (State->Template.StereoPlayback == eSampleStereo)
  842.                     {
  843.                         OutputPlace[0] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  844.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  845.                             OutputIndirectionTable,NumberOfOutputModulators));
  846.                         OutputPlace[1] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  847.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  848.                             OutputIndirectionTable,NumberOfOutputModulators));
  849.                     }
  850.                  else
  851.                     {
  852.                         OutputPlace[0] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  853.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  854.                             OutputIndirectionTable,NumberOfOutputModulators));
  855.                     }
  856.                 return 0;
  857.             }
  858.  
  859.         /* generate the sample position */
  860.         /* we want the modulation to be the same no matter how large the wavetable */
  861.         /* is, so we need to normalize the wave table index. */
  862.         Double2LongLong(ApplyModulation(LongLong2Double(State->WaveTableSamplePosition)
  863.             / State->FramesPerTable,PhaseGenModulateHow,PhaseGenModulationScaling,
  864.             PhaseGenModulationOrigin,PhaseGenOldValues,PhaseGenIndirectionTable,
  865.             NumberOfPhaseGenModulators) * State->FramesPerTable,FrameIndex);
  866.  
  867.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  868.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  869.             "WaveTableGenOneSample:  wave index out of range"));
  870.  
  871.         /* big selector of the thing to do */
  872.         if (State->Template.InterpolateAcrossWaves)
  873.             {
  874.                 /* wave & time interp */
  875.                 /* do the stuff for one wave table */
  876.                 if (State->TableNumBits == eSample16bit)
  877.                     {
  878.                         /* 16-bit sampling, wave interp, time interp */
  879.  
  880.                         signed short*                WaveData0;
  881.                         signed short*                WaveData1;
  882.                         FastFixedType                Wave0Weight;
  883.  
  884.                         FastFixedType                LeftWeight;
  885.                         long                                ArraySubscript;
  886.                         signed long                    Left0Value;
  887.                         signed long                    Right0Value;
  888.                         signed long                    Left1Value;
  889.                         signed long                    Right1Value;
  890.                         FastFixedType                Wave0Temp;
  891.  
  892.                         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  893.                             {
  894.                                 /* this is done in case the wave table index is at the maximum, */
  895.                                 /* in which case there is no table+1 to interpolate with. */
  896.                                 goto W16BitTimeNoWave;
  897.                             }
  898.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  899.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  900.                             State->WaveTableIndex)]));
  901.                         WaveData0 = (signed short*)(State->WaveTableMatrix[
  902.                             FastFixed2Int(State->WaveTableIndex)]);
  903.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  904.                             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  905.                             State->WaveTableIndex) + 1]));
  906.                         WaveData1 = (signed short*)(State->WaveTableMatrix[
  907.                             FastFixed2Int(State->WaveTableIndex) + 1]);
  908.                         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  909.  
  910.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  911.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  912.                         /* L+F(R-L) -- applied twice */
  913.                         Left0Value = WaveData0[ArraySubscript];
  914.                         Right0Value = WaveData0[ArraySubscript + 1];
  915.                         Left1Value = WaveData1[ArraySubscript];
  916.                         Right1Value = WaveData1[ArraySubscript + 1];
  917.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  918.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  919.                             * (Right1Value - Left1Value)) >> 15)) - Wave0Temp) >> 15);
  920.                     }
  921.                  else
  922.                     {
  923.                         /* 8-bit sampling, wave interp, time interp */
  924.  
  925.                         signed char*                WaveData0;
  926.                         signed char*                WaveData1;
  927.                         FastFixedType                Wave0Weight;
  928.  
  929.                         FastFixedType                LeftWeight;
  930.                         long                                ArraySubscript;
  931.                         signed long                    Left0Value;
  932.                         signed long                    Right0Value;
  933.                         signed long                    Left1Value;
  934.                         signed long                    Right1Value;
  935.                         FastFixedType                Wave0Temp;
  936.  
  937.                         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  938.                             {
  939.                                 /* this is done in case the wave table index is at the maximum, */
  940.                                 /* in which case there is no table+1 to interpolate with. */
  941.                                 goto W8BitTimeNoWave;
  942.                             }
  943.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  944.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  945.                             State->WaveTableIndex)]));
  946.                         WaveData0 = (signed char*)(State->WaveTableMatrix[
  947.                             FastFixed2Int(State->WaveTableIndex)]);
  948.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  949.                             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  950.                             State->WaveTableIndex) + 1]));
  951.                         WaveData1 = (signed char*)(State->WaveTableMatrix[
  952.                             FastFixed2Int(State->WaveTableIndex) + 1]);
  953.                         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  954.  
  955.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  956.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  957.                         /* L+F(R-L) -- applied twice */
  958.                         Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  959.                         Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  960.                         Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  961.                         Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  962.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  963.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  964.                             * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  965.                     }
  966.             }
  967.         else if (State->Template.InterpolateThroughTime)
  968.             {
  969.                 /* time interp */
  970.                 /* do the stuff for one wave table */
  971.                 if (State->TableNumBits == eSample16bit)
  972.                     {
  973.                         /* 16-bit sampling, time interp */
  974.  
  975.                         signed short*                WaveData;
  976.  
  977.                         FastFixedType                LeftWeight;
  978.                         long                                ArraySubscript;
  979.                         signed long                    LeftValue;
  980.                         signed long                    RightValue;
  981.  
  982.                      W16BitTimeNoWave:
  983.  
  984.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  985.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  986.                             State->WaveTableIndex)]));
  987.                         WaveData = (signed short*)(State->WaveTableMatrix[
  988.                             FastFixed2Int(State->WaveTableIndex)]);
  989.  
  990.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  991.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  992.                         /* L+F(R-L) */
  993.                         LeftValue = WaveData[ArraySubscript];
  994.                         RightValue = WaveData[ArraySubscript + 1];
  995.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  996.                     }
  997.                  else
  998.                     {
  999.                         /* 8-bit sampling, time interp */
  1000.  
  1001.                         signed char*                WaveData;
  1002.  
  1003.                         FastFixedType                LeftWeight;
  1004.                         long                                ArraySubscript;
  1005.                         signed long                    LeftValue;
  1006.                         signed long                    RightValue;
  1007.  
  1008.                      W8BitTimeNoWave:
  1009.  
  1010.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1011.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1012.                             State->WaveTableIndex)]));
  1013.                         WaveData = (signed char*)(State->WaveTableMatrix[
  1014.                             FastFixed2Int(State->WaveTableIndex)]);
  1015.  
  1016.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1017.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1018.                         /* L+F(R-L) */
  1019.                         LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1020.                         RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1021.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1022.                     }
  1023.             }
  1024.         else
  1025.             {
  1026.                 /* no interp */
  1027.                 /* do the stuff for one wave table */
  1028.                 if (State->TableNumBits == eSample16bit)
  1029.                     {
  1030.                         /* 16-bit sampling */
  1031.  
  1032.                         signed short*                WaveData;
  1033.  
  1034.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1035.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1036.                             State->WaveTableIndex)]));
  1037.                         WaveData = (signed short*)(State->WaveTableMatrix[
  1038.                             FastFixed2Int(State->WaveTableIndex)]);
  1039.  
  1040.                         Final = WaveData[LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1)];
  1041.                     }
  1042.                  else
  1043.                     {
  1044.                         /* 8-bit sampling */
  1045.  
  1046.                         signed char*                WaveData;
  1047.  
  1048.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1049.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1050.                             State->WaveTableIndex)]));
  1051.                         WaveData = (signed char*)(State->WaveTableMatrix[
  1052.                             FastFixed2Int(State->WaveTableIndex)]);
  1053.  
  1054.                         Final = WaveData[LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1)];
  1055.                     }
  1056.             }
  1057.  
  1058.         if (State->Template.StereoPlayback == eSampleStereo)
  1059.             {
  1060.                 ReturnValue = ((float)Final / MAX16BIT) * ((FastFixed2Float(
  1061.                     State->LeftLoudness) + FastFixed2Float(State->RightLoudness)) / 2);
  1062.                 if (State->Template.IncludeInFinalOutput)
  1063.                     {
  1064.                         OutputPlace[0] += double2largefixed(ApplyModulation(largefixed2single(
  1065.                             FastFixedTimes16BitTo24Bit((signed long)(State->LeftLoudness
  1066.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1067.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1068.                             OutputIndirectionTable,NumberOfOutputModulators));;
  1069.                         OutputPlace[1] += double2largefixed(ApplyModulation(largefixed2single(
  1070.                             FastFixedTimes16BitTo24Bit((signed long)(State->RightLoudness
  1071.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1072.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1073.                             OutputIndirectionTable,NumberOfOutputModulators));
  1074.                     }
  1075.             }
  1076.          else
  1077.             {
  1078.                 ReturnValue = ((float)Final / MAX16BIT)
  1079.                     * FastFixed2Float(State->OverallLoudness);
  1080.                 if (State->Template.IncludeInFinalOutput)
  1081.                     {
  1082.                         OutputPlace[0] += double2largefixed(ApplyModulation(largefixed2single(
  1083.                             FastFixedTimes16BitTo24Bit((signed long)(State->OverallLoudness
  1084.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1085.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1086.                             OutputIndirectionTable,NumberOfOutputModulators));
  1087.                     }
  1088.             }
  1089.  
  1090.         LongLongAdd(&State->WaveTableSamplePosition,
  1091.             &(State->WaveTableSamplePositionDifferential));
  1092.         LongLongMaskHighHalf(State->WaveTableSamplePosition,State->FramesPerTable - 1);
  1093.  
  1094.         return ReturnValue;
  1095.     }
  1096.  
  1097.  
  1098. static void                        Wave_Mono_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1099.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1100.     {
  1101.         LongLongRec                    LocalWaveTableSamplePosition;
  1102.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1103.         unsigned long                LocalSamplePositionMask;
  1104.         signed char*                WaveData;
  1105.         FastFixedType                LocalOverallLoudness;
  1106.  
  1107.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1108.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1109.             "Wave_Mono_8BitIn_NoTime_NoWave:  wave index out of range"));
  1110.         LocalOverallLoudness = State->OverallLoudness;
  1111.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1112.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1113.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1114.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1115.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1116.             State->WaveTableIndex)]));
  1117.         WaveData = (signed char*)(State->WaveTableMatrix[
  1118.             FastFixed2Int(State->WaveTableIndex)]);
  1119.  
  1120.         while (SampleCount > 0)
  1121.             {
  1122.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalOverallLoudness,
  1123.                     WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1124.                     & LocalSamplePositionMask]);
  1125.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1126.                     &LocalWaveTableSamplePositionDifferential);
  1127.                 SampleCount -= 1;
  1128.             }
  1129.  
  1130.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1131.     }
  1132.  
  1133.  
  1134. static void                        Wave_Stereo_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1135.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1136.     {
  1137.         LongLongRec                    LocalWaveTableSamplePosition;
  1138.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1139.         unsigned long                LocalSamplePositionMask;
  1140.         signed char*                WaveData;
  1141.         FastFixedType                LocalLeftLoudness;
  1142.         FastFixedType                LocalRightLoudness;
  1143.  
  1144.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1145.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1146.             "Wave_Stereo_8BitIn_NoTime_NoWave:  wave index out of range"));
  1147.         LocalLeftLoudness = State->LeftLoudness;
  1148.         LocalRightLoudness = State->RightLoudness;
  1149.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1150.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1151.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1152.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1153.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1154.             State->WaveTableIndex)]));
  1155.         WaveData = (signed char*)(State->WaveTableMatrix[
  1156.             FastFixed2Int(State->WaveTableIndex)]);
  1157.  
  1158.         while (SampleCount > 0)
  1159.             {
  1160.                 signed long                    SamplePoint;
  1161.  
  1162.                 SamplePoint = WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1163.                     & LocalSamplePositionMask];
  1164.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalLeftLoudness,SamplePoint);
  1165.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalRightLoudness,SamplePoint);
  1166.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1167.                     &LocalWaveTableSamplePositionDifferential);
  1168.                 SampleCount -= 1;
  1169.             }
  1170.  
  1171.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1172.     }
  1173.  
  1174.  
  1175. static void                        Wave_Mono_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1176.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1177.     {
  1178.         LongLongRec                    LocalWaveTableSamplePosition;
  1179.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1180.         unsigned long                LocalSamplePositionMask;
  1181.         signed short*                WaveData;
  1182.         FastFixedType                LocalOverallLoudness;
  1183.  
  1184.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1185.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1186.             "Wave_Mono_16BitIn_NoTime_NoWave:  wave index out of range"));
  1187.         LocalOverallLoudness = State->OverallLoudness;
  1188.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1189.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1190.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1191.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1192.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1193.             State->WaveTableIndex)]));
  1194.         WaveData = (signed short*)(State->WaveTableMatrix[
  1195.             FastFixed2Int(State->WaveTableIndex)]);
  1196.  
  1197.         while (SampleCount > 0)
  1198.             {
  1199.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1200.                     WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1201.                     & LocalSamplePositionMask]);
  1202.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1203.                     &LocalWaveTableSamplePositionDifferential);
  1204.                 SampleCount -= 1;
  1205.             }
  1206.  
  1207.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1208.     }
  1209.  
  1210.  
  1211. static void                        Wave_Stereo_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1212.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1213.     {
  1214.         LongLongRec                    LocalWaveTableSamplePosition;
  1215.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1216.         unsigned long                LocalSamplePositionMask;
  1217.         signed short*                WaveData;
  1218.         FastFixedType                LocalLeftLoudness;
  1219.         FastFixedType                LocalRightLoudness;
  1220.  
  1221.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1222.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1223.             "Wave_Stereo_16BitIn_NoTime_NoWave:  wave index out of range"));
  1224.         LocalLeftLoudness = State->LeftLoudness;
  1225.         LocalRightLoudness = State->RightLoudness;
  1226.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1227.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1228.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1229.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1230.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1231.             State->WaveTableIndex)]));
  1232.         WaveData = (signed short*)(State->WaveTableMatrix[
  1233.             FastFixed2Int(State->WaveTableIndex)]);
  1234.  
  1235.         while (SampleCount > 0)
  1236.             {
  1237.                 signed long                    SamplePoint;
  1238.  
  1239.                 SamplePoint = WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1240.                     & LocalSamplePositionMask];
  1241.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,SamplePoint);
  1242.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,SamplePoint);
  1243.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1244.                     &LocalWaveTableSamplePositionDifferential);
  1245.                 SampleCount -= 1;
  1246.             }
  1247.  
  1248.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1249.     }
  1250.  
  1251.  
  1252. static void                        Wave_Mono_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1253.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1254.     {
  1255.         LongLongRec                    LocalWaveTableSamplePosition;
  1256.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1257.         unsigned long                LocalSamplePositionMask;
  1258.         signed char*                WaveData;
  1259.         FastFixedType                LocalOverallLoudness;
  1260.  
  1261.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1262.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1263.             "Wave_Mono_8BitIn_YesTime_NoWave:  wave index out of range"));
  1264.         LocalOverallLoudness = State->OverallLoudness;
  1265.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1266.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1267.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1268.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1269.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1270.             State->WaveTableIndex)]));
  1271.         WaveData = (signed char*)(State->WaveTableMatrix[
  1272.             FastFixed2Int(State->WaveTableIndex)]);
  1273.  
  1274.         while (SampleCount > 0)
  1275.             {
  1276.                 FastFixedType                LeftWeight;
  1277.                 long                                ArraySubscript;
  1278.                 signed long                    LeftValue;
  1279.                 signed long                    RightValue;
  1280.  
  1281.                 /* naive anti-aliasing worked as follows: */
  1282.                 /*  L = left sample, R = right sample, F = fractional portion of index */
  1283.                 /*  S = L(1-F) + RF */
  1284.                 /*  Craig Peeper suggested the following optimization: */
  1285.                 /*  L(1-F)+RF ==> L-LF+RF ==> L+RF-LF ==> L+F(R-L) */
  1286.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1287.                     >> (32 - FASTFIXEDPRECISION);
  1288.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1289.                     & LocalSamplePositionMask;
  1290.                 /* L+F(R-L) */
  1291.                 LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1292.                 RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1293.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,LeftValue
  1294.                     + ((LeftWeight * (RightValue - LeftValue)) >> 15));
  1295.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1296.                     &LocalWaveTableSamplePositionDifferential);
  1297.                 SampleCount -= 1;
  1298.             }
  1299.  
  1300.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1301.     }
  1302.  
  1303.  
  1304. static void                        Wave_Stereo_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1305.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1306.     {
  1307.         LongLongRec                    LocalWaveTableSamplePosition;
  1308.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1309.         unsigned long                LocalSamplePositionMask;
  1310.         signed char*                WaveData;
  1311.         FastFixedType                LocalLeftLoudness;
  1312.         FastFixedType                LocalRightLoudness;
  1313.  
  1314.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1315.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1316.             "Wave_Stereo_8BitIn_YesTime_NoWave:  wave index out of range"));
  1317.         LocalLeftLoudness = State->LeftLoudness;
  1318.         LocalRightLoudness = State->RightLoudness;
  1319.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1320.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1321.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1322.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1323.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1324.             State->WaveTableIndex)]));
  1325.         WaveData = (signed char*)(State->WaveTableMatrix[
  1326.             FastFixed2Int(State->WaveTableIndex)]);
  1327.  
  1328.         while (SampleCount > 0)
  1329.             {
  1330.                 FastFixedType                LeftWeight;
  1331.                 long                                ArraySubscript;
  1332.                 signed long                    LeftValue;
  1333.                 signed long                    RightValue;
  1334.                 signed long                    CombinedValue;
  1335.  
  1336.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1337.                     >> (32 - FASTFIXEDPRECISION);
  1338.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1339.                     & LocalSamplePositionMask;
  1340.                 /* L+F(R-L) */
  1341.                 LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1342.                 RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1343.                 CombinedValue = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1344.                 /* Oh, Please Mr. Compiler, LeftValue and RightValue are dead now!!! */
  1345.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,CombinedValue);
  1346.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,CombinedValue);
  1347.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1348.                     &LocalWaveTableSamplePositionDifferential);
  1349.                 SampleCount -= 1;
  1350.             }
  1351.  
  1352.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1353.     }
  1354.  
  1355.  
  1356. static void                        Wave_Mono_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1357.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1358.     {
  1359.         LongLongRec                    LocalWaveTableSamplePosition;
  1360.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1361.         unsigned long                LocalSamplePositionMask;
  1362.         signed short*                WaveData;
  1363.         FastFixedType                LocalOverallLoudness;
  1364.  
  1365.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1366.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1367.             "Wave_Mono_16BitIn_YesTime_NoWave:  wave index out of range"));
  1368.         LocalOverallLoudness = State->OverallLoudness;
  1369.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1370.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1371.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1372.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1373.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1374.             State->WaveTableIndex)]));
  1375.         WaveData = (signed short*)(State->WaveTableMatrix[
  1376.             FastFixed2Int(State->WaveTableIndex)]);
  1377.  
  1378.         while (SampleCount > 0)
  1379.             {
  1380.                 FastFixedType                LeftWeight;
  1381.                 long                                ArraySubscript;
  1382.                 signed long                    LeftValue;
  1383.                 signed long                    RightValue;
  1384.  
  1385.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1386.                     >> (32 - FASTFIXEDPRECISION);
  1387.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1388.                     & LocalSamplePositionMask;
  1389.                 /* L+F(R-L) */
  1390.                 LeftValue = WaveData[ArraySubscript];
  1391.                 RightValue = WaveData[ArraySubscript + 1];
  1392.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,LeftValue
  1393.                     + ((LeftWeight * (RightValue - LeftValue)) >> 15));
  1394.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1395.                     &LocalWaveTableSamplePositionDifferential);
  1396.                 SampleCount -= 1;
  1397.             }
  1398.  
  1399.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1400.     }
  1401.  
  1402.  
  1403. static void                        Wave_Stereo_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1404.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1405.     {
  1406.         LongLongRec                    LocalWaveTableSamplePosition;
  1407.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1408.         unsigned long                LocalSamplePositionMask;
  1409.         signed short*                WaveData;
  1410.         FastFixedType                LocalLeftLoudness;
  1411.         FastFixedType                LocalRightLoudness;
  1412.  
  1413.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1414.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1415.             "Wave_Stereo_16BitIn_YesTime_NoWave:  wave index out of range"));
  1416.         LocalLeftLoudness = State->LeftLoudness;
  1417.         LocalRightLoudness = State->RightLoudness;
  1418.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1419.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1420.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1421.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1422.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1423.             State->WaveTableIndex)]));
  1424.         WaveData = (signed short*)(State->WaveTableMatrix[
  1425.             FastFixed2Int(State->WaveTableIndex)]);
  1426.  
  1427.         while (SampleCount > 0)
  1428.             {
  1429.                 FastFixedType                LeftWeight;
  1430.                 long                                ArraySubscript;
  1431.                 signed long                    LeftValue;
  1432.                 signed long                    RightValue;
  1433.                 signed long                    CombinedValue;
  1434.  
  1435.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1436.                     >> (32 - FASTFIXEDPRECISION);
  1437.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1438.                     & LocalSamplePositionMask;
  1439.                 /* L+F(R-L) */
  1440.                 LeftValue = WaveData[ArraySubscript];
  1441.                 RightValue = WaveData[ArraySubscript + 1];
  1442.                 CombinedValue = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1443.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,CombinedValue);
  1444.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,CombinedValue);
  1445.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1446.                     &LocalWaveTableSamplePositionDifferential);
  1447.                 SampleCount -= 1;
  1448.             }
  1449.  
  1450.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1451.     }
  1452.  
  1453.  
  1454. static void                        Wave_Mono_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1455.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1456.     {
  1457.         LongLongRec                    LocalWaveTableSamplePosition;
  1458.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1459.         unsigned long                LocalSamplePositionMask;
  1460.         signed char*                WaveData0;
  1461.         signed char*                WaveData1;
  1462.         FastFixedType                Wave0Weight;
  1463.         FastFixedType                LocalOverallLoudness;
  1464.  
  1465.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1466.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1467.             "Wave_Mono_8BitIn_YesTime_YesWave:  wave index out of range"));
  1468.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1469.             {
  1470.                 /* this is done in case the wave table index is at the maximum, in which */
  1471.                 /* case there is no table+1 to interpolate with. */
  1472.                 Wave_Mono_8BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1473.                 return;
  1474.             }
  1475.         LocalOverallLoudness = State->OverallLoudness;
  1476.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1477.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1478.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1479.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1480.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1481.             State->WaveTableIndex)]));
  1482.         WaveData0 = (signed char*)(State->WaveTableMatrix[
  1483.             FastFixed2Int(State->WaveTableIndex)]);
  1484.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1485.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1486.             State->WaveTableIndex) + 1]));
  1487.         WaveData1 = (signed char*)(State->WaveTableMatrix[
  1488.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1489.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1490.  
  1491.         while (SampleCount > 0)
  1492.             {
  1493.                 FastFixedType                LeftWeight;
  1494.                 long                                ArraySubscript;
  1495.                 signed long                    Left0Value;
  1496.                 signed long                    Right0Value;
  1497.                 signed long                    Left1Value;
  1498.                 signed long                    Right1Value;
  1499.                 FastFixedType                Wave0Temp;
  1500.  
  1501.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1502.                     >> (32 - FASTFIXEDPRECISION);
  1503.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1504.                     & LocalSamplePositionMask;
  1505.                 /* L+F(R-L) -- applied twice */
  1506.                 Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  1507.                 Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  1508.                 Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  1509.                 Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  1510.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1511.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1512.                     Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1513.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15));
  1514.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1515.                     &LocalWaveTableSamplePositionDifferential);
  1516.                 SampleCount -= 1;
  1517.             }
  1518.  
  1519.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1520.     }
  1521.  
  1522.  
  1523. static void                        Wave_Stereo_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1524.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1525.     {
  1526.         LongLongRec                    LocalWaveTableSamplePosition;
  1527.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1528.         unsigned long                LocalSamplePositionMask;
  1529.         signed char*                WaveData0;
  1530.         signed char*                WaveData1;
  1531.         FastFixedType                Wave0Weight;
  1532.         FastFixedType                LocalLeftLoudness;
  1533.         FastFixedType                LocalRightLoudness;
  1534.  
  1535.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1536.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1537.             "Wave_Stereo_8BitIn_YesTime_YesWave:  wave index out of range"));
  1538.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1539.             {
  1540.                 /* this is done in case the wave table index is at the maximum, in which */
  1541.                 /* case there is no table+1 to interpolate with. */
  1542.                 Wave_Stereo_8BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1543.                 return;
  1544.             }
  1545.         LocalLeftLoudness = State->LeftLoudness;
  1546.         LocalRightLoudness = State->RightLoudness;
  1547.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1548.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1549.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1550.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1551.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1552.             State->WaveTableIndex)]));
  1553.         WaveData0 = (signed char*)(State->WaveTableMatrix[
  1554.             FastFixed2Int(State->WaveTableIndex)]);
  1555.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1556.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1557.             State->WaveTableIndex) + 1]));
  1558.         WaveData1 = (signed char*)(State->WaveTableMatrix[
  1559.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1560.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1561.  
  1562.         while (SampleCount > 0)
  1563.             {
  1564.                 FastFixedType                LeftWeight;
  1565.                 long                                ArraySubscript;
  1566.                 signed long                    Left0Value;
  1567.                 signed long                    Right0Value;
  1568.                 signed long                    Left1Value;
  1569.                 signed long                    Right1Value;
  1570.                 FastFixedType                Wave0Temp;
  1571.  
  1572.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1573.                     >> (32 - FASTFIXEDPRECISION);
  1574.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1575.                     & LocalSamplePositionMask;
  1576.                 /* L+F(R-L) -- applied twice */
  1577.                 Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  1578.                 Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  1579.                 Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  1580.                 Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  1581.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1582.                 Wave0Temp = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1583.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  1584.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,Wave0Temp);
  1585.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,Wave0Temp);
  1586.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1587.                     &LocalWaveTableSamplePositionDifferential);
  1588.                 SampleCount -= 1;
  1589.             }
  1590.  
  1591.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1592.     }
  1593.  
  1594.  
  1595. static void                        Wave_Mono_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1596.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1597.     {
  1598.         LongLongRec                    LocalWaveTableSamplePosition;
  1599.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1600.         unsigned long                LocalSamplePositionMask;
  1601.         signed short*                WaveData0;
  1602.         signed short*                WaveData1;
  1603.         FastFixedType                Wave0Weight;
  1604.         FastFixedType                LocalOverallLoudness;
  1605.  
  1606.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1607.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1608.             "Wave_Mono_16BitIn_YesTime_YesWave:  wave index out of range"));
  1609.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1610.             {
  1611.                 /* this is done in case the wave table index is at the maximum, in which */
  1612.                 /* case there is no table+1 to interpolate with. */
  1613.                 Wave_Mono_16BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1614.                 return;
  1615.             }
  1616.         LocalOverallLoudness = State->OverallLoudness;
  1617.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1618.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1619.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1620.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1621.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1622.             State->WaveTableIndex)]));
  1623.         WaveData0 = (signed short*)(State->WaveTableMatrix[
  1624.             FastFixed2Int(State->WaveTableIndex)]);
  1625.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1626.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1627.             State->WaveTableIndex) + 1]));
  1628.         WaveData1 = (signed short*)(State->WaveTableMatrix[
  1629.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1630.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1631.  
  1632.         while (SampleCount > 0)
  1633.             {
  1634.                 FastFixedType                LeftWeight;
  1635.                 long                                ArraySubscript;
  1636.                 signed long                    Left0Value;
  1637.                 signed long                    Right0Value;
  1638.                 signed long                    Left1Value;
  1639.                 signed long                    Right1Value;
  1640.                 FastFixedType                Wave0Temp;
  1641.  
  1642.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1643.                     >> (32 - FASTFIXEDPRECISION);
  1644.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1645.                     & LocalSamplePositionMask;
  1646.                 /* L+F(R-L) -- applied twice */
  1647.                 Left0Value = WaveData0[ArraySubscript];
  1648.                 Right0Value = WaveData0[ArraySubscript + 1];
  1649.                 Left1Value = WaveData1[ArraySubscript];
  1650.                 Right1Value = WaveData1[ArraySubscript + 1];
  1651.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1652.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1653.                     Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1654.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15));
  1655.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1656.                     &LocalWaveTableSamplePositionDifferential);
  1657.                 SampleCount -= 1;
  1658.             }
  1659.  
  1660.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1661.     }
  1662.  
  1663.  
  1664. static void                        Wave_Stereo_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1665.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1666.     {
  1667.         LongLongRec                    LocalWaveTableSamplePosition;
  1668.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1669.         unsigned long                LocalSamplePositionMask;
  1670.         signed short*                WaveData0;
  1671.         signed short*                WaveData1;
  1672.         FastFixedType                Wave0Weight;
  1673.         FastFixedType                LocalLeftLoudness;
  1674.         FastFixedType                LocalRightLoudness;
  1675.  
  1676.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1677.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1678.             "Wave_Stereo_16BitIn_YesTime_YesWave:  wave index out of range"));
  1679.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1680.             {
  1681.                 /* this is done in case the wave table index is at the maximum, in which */
  1682.                 /* case there is no table+1 to interpolate with. */
  1683.                 Wave_Stereo_16BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1684.                 return;
  1685.             }
  1686.         LocalLeftLoudness = State->LeftLoudness;
  1687.         LocalRightLoudness = State->RightLoudness;
  1688.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1689.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1690.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1691.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1692.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1693.             State->WaveTableIndex)]));
  1694.         WaveData0 = (signed short*)(State->WaveTableMatrix[
  1695.             FastFixed2Int(State->WaveTableIndex)]);
  1696.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1697.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1698.             State->WaveTableIndex) + 1]));
  1699.         WaveData1 = (signed short*)(State->WaveTableMatrix[
  1700.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1701.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1702.  
  1703.         while (SampleCount > 0)
  1704.             {
  1705.                 FastFixedType                LeftWeight;
  1706.                 long                                ArraySubscript;
  1707.                 signed long                    Left0Value;
  1708.                 signed long                    Right0Value;
  1709.                 signed long                    Left1Value;
  1710.                 signed long                    Right1Value;
  1711.                 FastFixedType                Wave0Temp;
  1712.  
  1713.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1714.                     >> (32 - FASTFIXEDPRECISION);
  1715.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1716.                     & LocalSamplePositionMask;
  1717.                 /* L+F(R-L) -- applied twice */
  1718.                 Left0Value = WaveData0[ArraySubscript];
  1719.                 Right0Value = WaveData0[ArraySubscript + 1];
  1720.                 Left1Value = WaveData1[ArraySubscript];
  1721.                 Right1Value = WaveData1[ArraySubscript + 1];
  1722.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1723.                 Wave0Temp = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  1724.                     * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  1725.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,Wave0Temp);
  1726.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,Wave0Temp);
  1727.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1728.                     &LocalWaveTableSamplePositionDifferential);
  1729.                 SampleCount -= 1;
  1730.             }
  1731.  
  1732.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1733.     }
  1734.  
  1735.  
  1736. static void                        Wave_NoOutput(WaveTableStateRec* State,
  1737.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1738.     {
  1739.     }
  1740.